<template>
  <div class="scroll_wrap_overflow" ref="wrapper">
    <div class="scroll_content" ref="scrollWidth">
      <div class="scroll_wrap">
        <div
          class="scroll_item"
          v-for="(item, index) in showImages"
          :key="index"
        >
          <Card :border="true" class="scroll_img">
            <el-image
              :src="item.src"
              lazy
              :preview-src-list="[item.src]"
              alt=""
              ref="curImg"
              class="zoom-image"
            />
          </Card>
          <div class="scroll_txt">{{ item.txt }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
export default {
  props: {
    showImages: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      scroll: null,
    };
  },
  mounted() {
    //创建BScroll对象并设置参数
    this.scroll = new BScroll(this.$refs.wrapper, {
      disableMouse: false, //启用鼠标拖动
      disableTouch: false, //启用手指触摸
      scrollX: true, //X轴滚动启用
      eventPassthrough: "vertical",
    });
    const scrollXEnd =
      (this.showImages.length - 2) * this.$refs.curImg[0].width;
    this.$refs.scrollWidth.style.width =
      this.showImages.length * this.$refs.curImg[0].width + 100 + "px";
    this.scroll.refresh();
    this.scroll.scrollTo(-scrollXEnd, 0, 10000);
    setTimeout(() => {
      this.scroll.scrollTo(0, 0, 10000);
    }, 10000);
  },
};
</script>

<style lang="scss" scoped>
.scroll_wrap_overflow {
  width: 70%;
  margin: 0 auto;
  overflow: hidden;
  display: flex;
  justify-content: center;
  @media screen and (max-width: 768px) {
    width: 90%;
  }
  .scroll_content {
    .scroll_wrap {
      width: 100%;
      display: flex;
      @media screen and (max-width: 768px) {
        width: 100%;
        display: flex;
      }
      .scroll_item {
        width: 450px;
        margin-top: 30px;
        margin-right: 30px;
        flex-shrink: 0;
        @media screen and (max-width: 768px) {
          width: 180px;
          margin-right: 20px;
        }
        .scroll_img {
          width: 100%;
          @media screen and (max-width: 768px) {
            width: 180px;
          }
          img {
            width: 100%;
            @media screen and (max-width: 768px) {
              width: 100%;
            }
          }
        }
        .scroll_txt {
          display: flex;
          align-items: center;
          justify-content: center;
          color: #646464;
          font-size: 20px;
          margin-top: 30px;
          @media screen and (max-width: 768px) {
            font-size: 15px;
            width: 180px;
          }
        }
      }
    }
  }
  .scroll_bottom_btn {
    text-align: center;
    .bottom_btn {
      margin-top: 70px;
      background: orange;
      color: #fff;
      font-size: 13px;
      font-weight: bold;
      @media screen and (max-width: 768px) {
        border-radius: 20px;
        width: 140px;
        height: 40px;
        font-size: 14px;
      }
    }
  }
}
</style>
